package cn.fl.ref.fribaseinfo.service.impl;

import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itextpdf.text.pdf.PdfStructTreeController.returnType;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.ref.fribaseinfo.service.IFundRasInsLimService;
import cn.fl.ref.fribaseinfo.transform.FundRasInsLimVOTransform;
import cn.fl.ref.fribaseinfo.vo.FundRasInsLimVO;
import fl.ref.fribaseinfo.condition.FundRasInsLimQueryCondition;
import fl.ref.fribaseinfo.dto.FundRasInsLimDTO;
import fl.ref.fribaseinfo.service.IFundRasInsLimApiService;

/**
 * @Description: 资管机构限额
 */
@Service
public class FundRasInsLimServiceImpl  implements IFundRasInsLimService {

	private static final Logger logger = LoggerFactory.getLogger(FundRasInsLimServiceImpl.class);

	@Autowired
	private IFundRasInsLimApiService fundRasInsLimApiService;
	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	@Override
	public FundRasInsLimVO selectById(Long id) throws BaseException {
		if (StringUtils.isEmpty(id)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		FundRasInsLimVO FundRasInsLimVO = FundRasInsLimVOTransform.toVO(this.fundRasInsLimApiService.selectById(id));
		setDic(FundRasInsLimVO);
		return FundRasInsLimVO;
	}

	@Override
	public Long add(FundRasInsLimVO entity) throws BaseException {
		if (null == entity) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		
		Long defaultId = this.fundRasInsLimApiService.add(FundRasInsLimVOTransform.toDTO(entity));
		return defaultId;
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		if (StringUtils.isEmpty(id)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.fundRasInsLimApiService.removeById(id);
	}

	@Override
	public boolean modify(FundRasInsLimVO entity) throws BaseException {
		if (null == entity) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.fundRasInsLimApiService.modify(FundRasInsLimVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		if (null == page) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		Pager pager= this.fundRasInsLimApiService.searchListPage(page);
		List<FundRasInsLimVO> list= FundRasInsLimVOTransform.toVOList((List<FundRasInsLimDTO>)pager.getResultList());
		for (FundRasInsLimVO vo:list) {
			setDic(vo);
		}
		pager.setResultList(list);
		return pager;
	}

	@Override
	public List<FundRasInsLimVO> searchList(FundRasInsLimQueryCondition condition) throws BaseException {
		if (null == condition) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		List<FundRasInsLimVO> list= FundRasInsLimVOTransform.toVOList(this.fundRasInsLimApiService.searchList(condition));
		for (FundRasInsLimVO vo:list) {
			setDic(vo);
		}
		return list;
	}
	/**
	 * 设置数据字典
	 * @param vo
	 */
	 public void setDic(FundRasInsLimVO vo) {
		 List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(),
					CurrentThreadContext.getCurrentOrgCd(),  DictionaryConstant.CODE_PRJ_TYPE_CD);
			if (CollectionUtils.isNotEmpty(sysDictionaryDTOList)&&vo.getPrjTypCd()!=null) {
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					if (vo.getPrjTypCd().equals(sysDictionaryDTO.getCode())) {
						vo.setPrjTypCdNm(sysDictionaryDTO.getCodeNm());
					}
				}
			}
	 
	 }
}
